package com.mf.morefun.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.mf.morefun.R;
import com.mf.morefun.bean.WorksListBean;
import com.mf.morefun.widget.GlideRoundTransform;
import com.mf.morefun.widget.TransformationItemUtils;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import org.jetbrains.annotations.Nullable;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.support.constraint.Constraints.TAG;

public class WorksListAdapter extends BaseAdapter {
    private Context mContext;
    private List<WorksListBean> workList;

    public WorksListAdapter(Context mContext, List<WorksListBean> workList) {
        this.mContext = mContext;
        this.workList = workList;
    }

    @Override
    public int getCount() {
        return workList.size();
    }

    @Override
    public Object getItem(int position) {
        return workList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_workitem, null);
        } else {
            view = convertView;
        }
        if (null != workList && workList.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            holder.tvTime.setVisibility(View.GONE);
            if (null != workList.get(position).getBook()){
                //设置图片圆角角度
                RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 14));
                Glide.with(mContext).asBitmap().load(workList.get(position).getBook().getCover()).listener(new mRequestListener(holder)).apply(options).into(new TransformationItemUtils(holder.ivBookimg, mContext));
                if (StringUtils.isEmpty(workList.get(position).getBook().getName())){
                    holder.tvBookname.setText("");
                }else {
                    holder.tvBookname.setText(workList.get(position).getBook().getName());
                }
            }
            holder.tvTime.setText(workList.get(position).getUpdateDate());
        }
        return view;
    }
    private class mRequestListener implements RequestListener {
        private ViewHolder holder;
        public mRequestListener (ViewHolder holder){
            this.holder = holder;
        }
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
            Log.d(TAG, "onException: " + e.toString()+"  model:"+model+" isFirstResource: "+isFirstResource);
            Message msg = new Message();
            msg.what = 0;
            msg.obj = holder.ivBookimg;
            mHandler.sendMessage(msg);
            return false;
        }

        @Override
        public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            Log.e(TAG,  "model:"+model+" isFirstResource: "+isFirstResource);
            return false;
        }
    };

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    RequestOptions options = new RequestOptions().transform(new GlideRoundTransform(mContext, 14));
                    Glide.with(mContext).asBitmap().load(R.mipmap.mf_logo).apply(options).into(new TransformationItemUtils((ImageView)msg.obj, mContext));
                    break;
            }
        }
    };
    static class ViewHolder {
        @BindView(R.id.iv_bookimg)
        ImageView ivBookimg;
        @BindView(R.id.tv_bookname)
        TextView tvBookname;
        @BindView(R.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
